package com.ch.dygang;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public class Spider {

    static {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

            }
        },0,1000 * 60 * 60 * 24);
    }

    public static void capture() throws IOException {
        URL url = new URL("https://www.dygang.cc/");
        Document document = Jsoup.parse(url, 5000);
        Elements elements = document.select("[id^=tab1_div_].border");

        elements.forEach(element -> {
            //TODO 采集用户ID,name,image,intime

            Element a = element.selectFirst("table:nth-child(1) a");
            String href = a.attr("href");
            String type = href.replaceAll("/(\\w+)/.*","$1");
            String intime = href.replaceAll("/(\\w+)/(\\d+)/.*","$2");
            String id = href.replaceAll("/(\\w+)/(\\d)/(\\d+).*","$3");
            Element img = element.selectFirst("table:nth-child(1) a>img");
            String image = img.attr("src");
            String name = img.attr("alt");
            System.out.printf("%s, %s, %s, %s, %s\n",id, name, type, intime, image);

        });


    }
}
